package com.bdzl.admin.service.sys.impl;

import com.bdzl.pub.util.file.BASE64DecodedMultipartFile;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.bdzl.admin.dao.sys.OrgDao;
import com.bdzl.pub.entity.sys.OrgDO;
import com.bdzl.admin.service.sys.OrgService;
import org.springframework.web.multipart.MultipartFile;


@Service
public class OrgServiceImpl implements OrgService {
	@Autowired
	private OrgDao orgDao;
	
	@Override
	public OrgDO get(String orgId){
		return orgDao.get(orgId);
	}
	
	@Override
	public List<OrgDO> list(Map<String, Object> map){
		return orgDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return orgDao.count(map);
	}
	
	@Override
	public int save(OrgDO org){
		if (org.getOrgLogo()!=null && !org.getOrgLogo().startsWith("http")) {
			try {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(org.getOrgLogo());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "oan/org/org_logo");
				//修改头像
				org.setOrgLogo(docUrl);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return orgDao.save(org);
	}
	
	@Override
	public int update(OrgDO org){
		if (org.getOrgLogo()!=null && !org.getOrgLogo().startsWith("http")) {
			try {
				//上传百度云
				MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(org.getOrgLogo());
				String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(file1, "oan/org/org_logo");
				//修改头像
				org.setOrgLogo(docUrl);
				if (org.getOrgLogo() != null) {
					//获取旧头像
					String userHeadImg = orgDao.get(org.getOrgId()).getOrgLogo();
					if (userHeadImg != null && userHeadImg.startsWith("http")) {
						OssUploadFileUtil.remove(userHeadImg);
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return orgDao.update(org);
	}
	
	@Override
	public int remove(String orgId){
		return orgDao.remove(orgId);
	}
	
	@Override
	public int batchRemove(String[] orgIds){
		return orgDao.batchRemove(orgIds);
	}

}
